﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Design
{
    /// <summary>
    /// 单例模式
    /// </summary>

    public sealed class Singlton
    {
        //构造函数
        public Singlton() { }


        #region  A 单例模式简单实现
        static Singlton _instanceA = null;
        /// <summary>
        /// 线程不安全
        /// 对于多线程会有问题，因为Singlton 对象可能不指一次被创建，而罪魁祸首就是if (instance == null)这句话，它并不是线程安全的。
        /// </summary>
        public static Singlton InstanceA
        {
            get
            {
                if (_instanceA == null)
                {
                    _instanceA = new Singlton();
                }
                return _instanceA;
            }
        }
        #endregion


        #region B 线程安全模式
        volatile static Singlton _instanceB = null;
        static readonly object _obj = new object();
        /// <summary>
        /// 线程安全模式
        /// 使用 volatile 定义 _instanceB 属性，保证线程安全
        /// 加 lock 锁定对象，检查是否对象被创建
        /// </summary>
        public static Singlton InstanceB
        {
            get
            {
                if (_instanceB == null)
                {
                    lock (_obj)
                    {
                        if (_instanceB == null)
                        {
                            _instanceB = new Singlton();
                        }
                    }
                }
                return _instanceB;
            }
        }
        #endregion



        #region C 双重锁定
        static Singlton _instanceC = null;
        static readonly object _objC = new object();
        /// <summary>
        /// 双重锁定
        /// 解决线程并发问题，并避免每个InstanceC 属性方法调用中都出现lock 独占锁定
        /// 无法实现延迟初始化
        /// </summary>
        public static Singlton InstanceC
        {
            get
            {
                if (_instanceC == null)
                {
                    lock (_objC)
                    {
                        if (_instanceC == null)
                        {
                            _instanceC = new Singlton();
                        }
                    }
                }
                return _instanceC;
            }
        }

        #endregion

        #region D 静态初始化
        /// <summary>
        /// 静态初始化
        /// </summary>
        static readonly Singlton _instanceD = new Singlton();
        static Singlton() { }
        public static Singlton InstanceD
        {
            get
            {
                return _instanceD;
            }
        }
        #endregion


        #region E 延迟初始化
        /// <summary>
        /// 延迟初始化
        /// </summary>
        public static Singlton instanceE
        {
            get
            {
                return Nested.instance;
            }
        }
        #endregion

        public string Fun()
        {
            return "Hello world!";
        }
    }


    /// <summary>
    /// 单例模式 延迟初始化实现方式
    /// </summary>
    class Nested
    {
        static Nested() { }

        internal static readonly Singlton instance = new Singlton();
    }
}
